package com.fxf.srb.core.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxf.common.exception.Assert;
import com.fxf.common.result.R;
import com.fxf.common.result.ResponseEnum;
import com.fxf.common.util.RegexValidateUtils;
import com.fxf.srb.base.util.JwtUtils;
import com.fxf.srb.core.pojo.entity.UserInfo;
import com.fxf.srb.core.pojo.vo.LoginVo;
import com.fxf.srb.core.pojo.vo.RegisterVO;
import com.fxf.srb.core.pojo.vo.UserIndexVO;
import com.fxf.srb.core.pojo.vo.UserInfoVo;
import com.fxf.srb.core.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author fxf
 * @since 2021-05-26
 */
@RestController
@RequestMapping("/api/core/userInfo")
@Api(tags = "会员接口")
@CrossOrigin
@Slf4j
public class UserInfoController {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private IUserInfoService iUserInfoService;

    @ApiOperation("会员注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVO registerVO){

        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();

        //手机号不能为空
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //验证手机号格式
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);
        //密码不能为空
        Assert.notEmpty(password,ResponseEnum.PASSWORD_NULL_ERROR);
        //验证码不能为空
        Assert.notEmpty(code,ResponseEnum.CODE_NULL_ERROR);

        //校验验证码是否正确
        String o = (String) redisTemplate.opsForValue().get("srb:sms:code" + mobile);
        Assert.equals(code,o,ResponseEnum.CODE_ERROR);

        iUserInfoService.register(registerVO);

        return R.ok().message("用户注册成功");
    }

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo, HttpServletRequest request){

        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        Assert.notEmpty(mobile,ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(password,ResponseEnum.PASSWORD_NULL_ERROR);

        //获取请求的ip地址
        String ip = request.getRemoteAddr();

        UserInfoVo userInfoVo = iUserInfoService.login(loginVo,ip);

        return R.ok().data("userInfo",userInfoVo);
    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public R chectToken(HttpServletRequest request){

        String token = request.getHeader("token");
        boolean b = JwtUtils.checkToken(token);
        if (b){
            return R.ok();
        }else {
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }


    }
    @ApiOperation("校验手机号是否被注册")
    @GetMapping("/checkMobile/{mobile}")
    public boolean checkMobile(@PathVariable String mobile){

        return iUserInfoService.checkMobile(mobile);
    }

    @ApiOperation("获取个人空间用户信息")
    @GetMapping("/auth/getIndexUserInfo")
    public R getIndexUserInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        UserIndexVO userIndexVO = iUserInfoService.getIndexUserInfo(userId);
        return R.ok().data("userIndexVO",userIndexVO);
    }
}

